<template>
    <div class="device-list">
        <div class="device-list-container flex-container">


            <div class="device-list-table">

                <el-table :data="goodsSpecList" border>

                    <!--<el-table-column label="ID" prop="id" width="60"></el-table-column>-->
                    <el-table-column label="规格名称" prop="name" min-width="180"></el-table-column>
                    <el-table-column label="价格">
                        <template slot-scope="scope">
                            {{scope.row.price | currencyFormat}}
                        </template>
                    </el-table-column>
                    <el-table-column label="库存">
                        <template slot-scope="scope">
                            {{scope.row.stock}}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" fixed="right" width="200">
                        <template slot-scope="scope">
                            <el-button type="success outline" size="mini"
                                       @click="handleEdit(scope.$index, scope.row)">
                                编辑
                            </el-button>
                            <el-button size="mini" type="danger outline"
                                       @click="handleDelete(scope.$index, scope.row)">
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>

            <div class="flex-footer h-80">
                <div class="flex-footer-content">
                    <el-row>
                        <el-col :span="8" class="pt-6">&nbsp;
                            <el-button type="success" @click="handleAddGoods()">新增商品规格</el-button>
                        </el-col>
                        <el-col :span="16" class="text-right" v-if="total > 0">
                            <el-pagination
                                    @current-change="pageChange"
                                    :current-page="currentPage"
                                    :page-size="pageSize"
                                    layout="total, prev, pager, next, jumper"
                                    :total="total">
                            </el-pagination>
                        </el-col>
                        <!--<el-col :span="8" class="text-center" v-if="total > 0"></el-col>-->
                    </el-row>
                </div>
            </div>
        </div>

        <!--编辑弹窗-->
        <el-dialog title="商品编辑" custom-class="edit-dialog" width="40%" :visible.sync="editModalVisible"
                   @close="handleEditModalClose">
            <el-form :label-position="'left'" label-width="110px" ref="userForm" :model="goodsForm">
                <el-form-item label="规格名称">
                    <el-input v-model="goodsForm.name" placeholder="请输入规格名称"></el-input>
                </el-form-item>

                <el-form-item label="价格">
                    <el-input v-model="goodsForm.price" placeholder="">
                        <template slot="append">元</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="库存">
                    <el-input v-model="goodsForm.stock" placeholder=""></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="success" @click="saveGoods('goodsForm')">保存</el-button>
            </div>
        </el-dialog>


    </div>
</template>

<script type="text/ecmascript-6">

    import Core from 'core/core'

    export default {
        data() {
            return {
                Core: Core,
                editModalVisible: false,
                goodsForm: {
                    id: 0,
                    goodsId: 0,
                    name: "",
                    price: 0,
                    stock: 0
                },

                goodsIs: 0,

                coverImgUrl: "",
                imgListUrl: [],
                goodsSpecList: [],
                tpCodeList: [],

                // 分页配置
                currentPage: 1,
                total: 0,
                pageSize: 10,
            }
        },
        mounted: function () {
            this.goodsIs = this.$route.query.goods_id || 0;

            this._goodsSpecList();
        },

        methods: {

            _goodsSpecList() {
                Core.Api.Goods.goodsSpecList(this.goodsIs)
                    .then(res => {
                        this.goodsSpecList = res.list;
                    })
                    .catch(err => {
                    })
            },

            pageChange(val) {
                this.currentPage = val;
                this._goodsSpecList();
            },

            saveGoods(formName) {

                Core.Api.Goods.saveGoodsSpec(
                    this.goodsForm.id,
                    this.goodsIs,
                    this.goodsForm.name,
                    this.goodsForm.price * 100,
                    this.goodsForm.stock
                ).then(res => {
                    this.editModalVisible = false;
                    this.$message.success("保存成功");
                    this._goodsSpecList();
                })
            },

            handleDelete(index, row) {
                this.$alert('确认删除吗?', {
                    confirmButtonText: '确定',
                    callback: action => {
                        if (action == 'confirm') {
                            Core.Api.Goods.deleteGoodsSpec(row.id)
                                .then(res => {

                                    this.$message.success("删除成功");
                                    this._goodsSpecList();
                                })
                        }
                    }
                });
            },

            handleEdit(index, row) {
                this.goodsForm = {
                    id: row.id,
                    goodsId: row.goods_id,
                    name: row.name,
                    price: row.price / 100,
                    stock: row.stock
                };

                this.editModalVisible = true;
            },

            handleAddGoods() {
                this.editModalVisible = true;
            },

            handleEditModalClose() {
                Core.Util.clearData(this.goodsForm);

            },



        },


    }
</script>

<style scoped lang="scss">
    .device-list {
        height: 100%;
        .device-list-container {
            height: 100%;
            .search-row {
                margin: 0;
                padding: 15px 30px;
                background: #fff;
                .el-form--inline .el-form-item {
                    margin-bottom: 0;
                }
                .el-form-item__label {
                    padding-right: 10px;
                    font-size: 12px;
                }
                .el-form-item__content {
                    .el-input {
                        width: 175px;
                    }
                }
            }

            .device-list-table {
                padding: 20px 0;
                background: #ffffff;
                position: relative;
                flex: 1;
                overflow: auto;
                .all-select-holder {
                    line-height: 30px;
                    position: absolute;
                    left: 10px;
                    z-index: 10;
                }
            }
            .flex-footer {
                border-top: 1px solid #E0E7EF;
            }
            .flex-footer.h-80 {
                height: 80px;
            }

        }

        a.success {
            display: inline-block;
            color: #fff;
            background-color: #03CC75;
            border-color: #03CC75;
            line-height: 1;
            white-space: nowrap;
            cursor: pointer;
            -webkit-appearance: none;
            text-align: center;
            box-sizing: border-box;
            outline: none;
            -moz-user-select: none;
            -webkit-user-select: none;
            -ms-user-select: none;
            padding: 7px 16px;
            font-size: 12px;
            border-radius: 4px;
            margin: 0 10px;
        }

        .upload-device {
            display: inline-block;
        }
    }
</style>
